package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author manchi520
 * @date 2022/7/2
 * @apiNote
 */
@Controller
@RequestMapping("/web")
public class WebController {
    @RequestMapping("/p")
    @ResponseBody
    public Map<String,Map<String,String>> getParam(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String method = req.getMethod();
        Map<String,String> postParamMap = new HashMap<>();
        if (method.equals("POST")){
            ServletInputStream inputStream = req.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            resp.setContentType("text/html;charset=utf-8");
            String s = "";
            while ((s=br.readLine()) != null) {
                postParamMap.put("postParm",s);
            }
            br.close();
        }
        Map<String,String> paramMap = new HashMap<>();
        //获取请求头的参数名 然后获取相应的值
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String s = parameterNames.nextElement();
            paramMap.put(s,req.getParameter(s));
        }
        Map<String,String> headMap = new HashMap<>();
        //获取请求头的信息
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String s = headerNames.nextElement();
            headMap.put(s,req.getHeader(s));
        }

        Map<String,Map<String,String>> map = new HashMap<>();
        map.put("requestParam",paramMap);
        map.put("requestBody",headMap);
        map.put("postParamMap",postParamMap);
        return map;
    }
}
